//
//  ClassListViewController.m
//  cherryproduct
//
//  Created by umessage on 14-8-11.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "ClassListViewController.h"
#import "UIImageView+AFNetworking.h"
#import "HttpNewBaseService.h"
#import "BuyProductViewController.h"
#import "ClassListFooter.h"

@interface ClassListViewController ()

@property(nonatomic, assign) BOOL m_b_have_more;
@property(nonatomic, assign) BOOL m_b_is_loading;
@property(nonatomic, assign) NSInteger m_b_page_index;

@property(nonatomic, strong) UIView * view_no_data_b;

@end

@implementation ClassListViewController

@synthesize issearch;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        issearch = NO;
        isloadclass = NO;
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.m_b_have_more=YES;
    self.m_b_page_index=1;
    self.m_b_is_loading=YES;
    
    if (self.view_no_data_b == nil) {
        self.view_no_data_b = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.myView.frame.size.height)];
        self.view_no_data_b.backgroundColor=[UIColor whiteColor];
        self.view_no_data_b.hidden=YES;
        UILabel * label_no = [[UILabel alloc] initWithFrame:CGRectMake(0, (self.view_no_data_b.frame.size.height-64)*0.382-28, SCREEN_WIDTH, 30)];
        label_no.text=@"该分类下尚无商品!";
        label_no.font=[UIFont systemFontOfSize:17];
        label_no.textColor=RGBCOLOR(187, 187, 187);
        label_no.textAlignment=NSTextAlignmentCenter;
        [self.view_no_data_b addSubview:label_no];
        
        [self.myView addSubview:self.view_no_data_b];
    }
    
    if(self.issearch)
    {
        //搜索功能:
        [self setTitle:[dicdataparam objectForKey:@"kwords"]];
        [self.view startLoadingAnimation];
        
        [[HttpNewBaseService sharedClient] getRequestOperation:@"index/search" paramDictionary:dicdataparam onCompletion:^(NSString *responseString) {
            [self.view stopLoadingAnimation];
            @try {
                NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                
                NSDictionary * dic_data = [jsondic objectForKey:@"data"];
                
                if(dic_data && [dic_data objectForKey:@"goods"]&&([[dic_data objectForKey:@"goods"] isKindOfClass:[NSArray class]]))
                {
                    NSArray * arr = [dic_data objectForKey:@"goods"];
                    self.m_b_is_loading=NO;
                    if ([arr count] >= 10) {
                        self.m_b_have_more=YES;
                    } else {
                        self.m_b_have_more=NO;
                    }
                    
                    if (data_list) {
                        [data_list addObjectsFromArray:arr];
                    } else {
                        data_list = [[NSMutableArray alloc] initWithArray:arr copyItems:YES];
                    }
                    
                    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
                    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
                    flowLayout.footerReferenceSize=CGSizeMake(self.view.frame.size.width, 50);
                    //注册
                    collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width,self.myView.frame.size.height) collectionViewLayout:flowLayout];
                    [collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
                    collectionview.delegate = self;
                    collectionview.dataSource  = self;
                    collectionview.backgroundColor = [UIColor whiteColor];
                    [collectionview registerClass:[ClassListFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ClassListFooter"];
                    [self.myView addSubview:collectionview]; //分类列表
                    
                } else {
                    [self showAlertView:@"温馨提示" subtitle:@"没有找到对应的商品！"];
                }
            } @catch (NSException *exception) {
                [self showAlertView:@"温馨提示" subtitle:@"服务器异常！"];
            }@finally {}
        } onError:^(NSError *error) {
            [self.view stopLoadingAnimation];
            [self showAlertView:@"温馨提示" subtitle:@"网络断了！"];
        } onAnimated:YES]; //分类列表
    }
    else //分类列表
    {
        [self addNavBarButton:RIGHT_MORE];
        
        [self setTitle:[dicdataparam objectForKey:@"title"]];
        [dicdataparam removeObjectForKey:@"title"];
        if(self.issearch)
        {
            
        }
        else //分类列表
        {
            [self.view startLoadingAnimation];
            
            [[HttpNewBaseService sharedClient] getRequestOperation:@"goods/goods_list" paramDictionary:dicdataparam onCompletion:^(NSString *responseString) {
                [self.view stopLoadingAnimation];
                
                @try {
                    NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                    dicclasslist = [[NSMutableDictionary alloc] initWithDictionary:jsondic copyItems:YES];
                    
                    if([jsondic objectForKey:@"data"]&&([[[jsondic objectForKey:@"data"] objectForKey:@"goods"] isKindOfClass:[NSArray class]]))
                    {
                        NSArray * arr = [[jsondic objectForKey:@"data"] objectForKey:@"goods"];
                        self.m_b_is_loading=NO;
                        if ([arr count] >= 10) {
                            self.m_b_have_more=YES;
                        } else {
                            self.m_b_have_more=NO;
                        }
                        data_list_no_search=[[NSMutableArray alloc] initWithArray:arr copyItems:YES];
                        
                        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
                        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
                        flowLayout.footerReferenceSize=CGSizeMake(self.view.frame.size.width, 50);
                        //注册
                        collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width,self.myView.frame.size.height) collectionViewLayout:flowLayout];
                        [collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
                        collectionview.delegate = self;
                        collectionview.dataSource  = self;
                        collectionview.backgroundColor = [UIColor whiteColor];
                        
                        [collectionview registerClass:[ClassListFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ClassListFooter"];
                        [self.myView addSubview:collectionview]; //分类列表
                        
                        self.view_no_data_b.hidden=YES;
                    } else {
                        //[self showAlertView:@"温馨提示" subtitle:@"没有找到商品!"];
                        self.view_no_data_b.hidden=NO;
                    }
                } @catch (NSException *exception) {
                    [self showAlertView:@"温馨提示" subtitle:@"服务器异常！"];
                }
            } onError:^(NSError *error) {
                ;
                [self.view stopLoadingAnimation];
                [self showAlertView:@"温馨提示" subtitle:@"网络断了！"];
            } onAnimated:YES]; //分类列表
        }

    }
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath
{
    ClassListFooter * footer = nil;
    if([kind isEqual:UICollectionElementKindSectionFooter]){
        footer = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                    withReuseIdentifier:@"ClassListFooter"
                                                           forIndexPath:indexPath];
        if (self.m_b_have_more) {
            footer.label_title.text=@"正在加载";
            footer.active_loading.hidden=NO;
            [footer.active_loading startAnimating];
        } else {
            if (self.issearch) {
                footer.label_title.text=[NSString stringWithFormat:@"共%d个商品", [data_list count]];
                footer.active_loading.hidden=YES;
                [footer.active_loading stopLoadingAnimation];
            } else {
                footer.label_title.text=[NSString stringWithFormat:@"共%d个商品", [data_list_no_search count]];;
                footer.active_loading.hidden=YES;
                [footer.active_loading stopLoadingAnimation];
            }
        }
    }
    return footer;
}

#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.issearch) {
        if (data_list) {
            return [data_list count];
        }
        return 0;
    } else {
        if (data_list_no_search) {
            return [data_list_no_search count];
        }
        return 0;
        //return [[[dicclasslist objectForKey:@"data"] objectForKey:@"goods"] count];
    }
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* string = @"cell";
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];
    
    if (!self.issearch) {
        NSInteger index = indexPath.row;
        if (data_list_no_search && [data_list_no_search isKindOfClass:[NSMutableArray class]]) {
            if (index < [data_list_no_search count]) {
                
                NSDictionary * dic = [data_list_no_search objectAtIndex:indexPath.row];
                
                if([cell viewWithTag:102])
                {
                    UIImageView * imageview = (UIImageView*)[cell viewWithTag:102];
                    
                    NSString * stringurl = [NSString stringWithFormat:@"%@%@",JPG, [dic objectForKey:@"photo"]];

                    [imageview setImageWithURL:[NSURL URLWithString:stringurl] placeholderImage:[UIImage imageNamed:@"classlistloading"]];
                }
                else
                {
                    UIImageView* imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 130, 130)];
                    imageview.tag = 102;
                    
                    NSString * stringurl = [NSString stringWithFormat:@"%@%@",JPG, [dic objectForKey:@"photo"]];
                    
                    [imageview setImageWithURL:[NSURL URLWithString:stringurl] placeholderImage:[UIImage imageNamed:@"classlistloading"]];
                    
                    [cell addSubview:imageview];
                }
                
                if([cell viewWithTag:101])
                {
                    UILabel* label = (UILabel*)[cell viewWithTag:101];
                    label.text = [dic objectForKey:@"name"];
                }
                else
                {
                    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 130, 130, 40)];
                    label.tag = 101;
                    label.text = [dic objectForKey:@"name"];
                    label.textColor = [UIColor blackColor];
                    label.textAlignment = NSTextAlignmentLeft;
                    label.font = [UIFont systemFontOfSize:15];
                    label.numberOfLines = 0;
                    label.backgroundColor = [UIColor clearColor];
                    [cell addSubview:label];
                }
            }
        }
    } else {
        if (data_list) {
            NSInteger index = indexPath.row;
            if (index < [data_list count]) {
                NSDictionary * dic = [data_list objectAtIndex:indexPath.row];
                //设置数据
                if ([cell viewWithTag:102])
                {
                    UIImageView* imageview = (UIImageView*)[cell viewWithTag:102];
                    NSString* stringurl = [NSString stringWithFormat:@"%@%@",JPG,[dic objectForKey:@"photo"]];
                    [imageview setImageWithURL:[NSURL URLWithString:stringurl]
                              placeholderImage:[UIImage imageNamed:@"classlistloading"]];
                }
                else
                {
                    UIImageView* imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 130, 130)];
                    imageview.tag = 102;
                    NSString* stringurl = [NSString stringWithFormat:@"%@%@",JPG,[dic objectForKey:@"photo"]];
                    [imageview setImageWithURL:[NSURL URLWithString:stringurl]
                              placeholderImage:[UIImage imageNamed:@"classlistloading"]];
                    [cell addSubview:imageview];
                }
                if([cell viewWithTag:101])
                {
                    UILabel* label = (UILabel*)[cell viewWithTag:101];
                    label.text = [dic objectForKey:@"name"];
                }
                else
                {
                    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 130, 130, 40)];
                    label.tag = 101;
                    label.text = [dic objectForKey:@"name"];
                    label.textColor = [UIColor blackColor];
                    label.textAlignment = NSTextAlignmentLeft;
                    label.font = [UIFont systemFontOfSize:15];
                    label.numberOfLines = 0;
                    label.backgroundColor = [UIColor clearColor];
                    [cell addSubview:label];
                }
            }
        }
    }
    
    return cell;
}
#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(130, 170); //image130
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(15, 20, 15, 20);
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"click index:%d", indexPath.row);
    BuyProductViewController* buy = [[BuyProductViewController alloc] init];
    
    if (!self.issearch) {
        NSInteger index = indexPath.row;
        
        if (index < [data_list_no_search count]) {
            NSDictionary * dic = [data_list_no_search objectAtIndex:indexPath.row];
            NSString * str_id = [dic objectForKey:@"id"];
            if (str_id==nil) {
                str_id=@"1";
            }
            buy.stringid = str_id;
            
            buy.m_b_is_come_s=NO;
            buy.m_b_is_have_say=NO;
            NSDictionary * dic_groupon_status = [dic objectForKey:@"groupon_status"];
            if (dic_groupon_status &&
                [dic_groupon_status isKindOfClass:[NSDictionary class]]) {
                
                buy.m_b_is_come_s=[[dic_groupon_status objectForKey:@"is_groupon"] boolValue];
                buy.m_b_is_have_say = [[dic_groupon_status objectForKey:@"is_commented"] boolValue];
                
                buy.str_group_id=[dic_groupon_status objectForKey:@"id_groupon"];
                buy.end_time = [[dic_groupon_status objectForKey:@"end_time"] doubleValue];
                if ([dic_groupon_status objectForKey:@"price"] && [[dic_groupon_status objectForKey:@"price"] isKindOfClass:[NSString class]]) {
                    buy.str_money=[dic_groupon_status objectForKey:@"price"];
                }
            }
            
            NSString * buy_url = [dic objectForKey:@"buy_url"];
            if (buy_url == nil) {
                buy_url = @"http://www.taobao.com";
            }
            buy.stringurl = buy_url;
            
            //share
            buy.str_share_img_url = [NSString stringWithFormat:@"%@%@",JPG, [dic objectForKey:@"photo"]];
            buy.str_share_name = [dic objectForKey:@"name"];
        }
        
    } else {
        NSDictionary * dic = [data_list objectAtIndex:indexPath.row];
        NSString * str_id = [dic objectForKey:@"id"];
        if (str_id==nil) {
            str_id=@"1";
        }
        buy.stringid = str_id;
        
        buy.m_b_is_come_s=NO;
        buy.m_b_is_have_say=NO;
        NSDictionary * dic_groupon_status = [dic objectForKey:@"groupon_status"];
        if (dic_groupon_status &&
            [dic_groupon_status isKindOfClass:[NSDictionary class]]) {
            
            buy.m_b_is_come_s=[[dic_groupon_status objectForKey:@"is_groupon"] boolValue];
            buy.m_b_is_have_say = [[dic_groupon_status objectForKey:@"is_commented"] boolValue];
            
            buy.str_group_id=[dic_groupon_status objectForKey:@"id_groupon"];
            buy.end_time = [[dic_groupon_status objectForKey:@"end_time"] doubleValue];
            
            if ([dic_groupon_status objectForKey:@"price"] && [[dic_groupon_status objectForKey:@"price"] isKindOfClass:[NSString class]]) {
                buy.str_money=[dic_groupon_status objectForKey:@"price"];
            }
        }
        
        NSString * buy_url = [dic objectForKey:@"buy_url"];
        if (buy_url == nil) {
            buy_url = @"http://www.taobao.com";
        }
        buy.stringurl = buy_url;
        
        //share
        buy.str_share_img_url = [NSString stringWithFormat:@"%@%@",JPG, [dic objectForKey:@"photo"]];
        buy.str_share_name = [dic objectForKey:@"name"];
    }
    
    UIImageView * imageview = (UIImageView *)[[collectionView cellForItemAtIndexPath:indexPath] viewWithTag:102];
    if (imageview) {
        buy.image_share_ = imageview.image;
    }
    
    [self.navigationController pushViewController:buy animated:YES];
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
 
}

-(void)getuseparamclasslistdata:(NSDictionary *)dic
{
    dicdataparam = [[NSMutableDictionary alloc] initWithDictionary:dic copyItems:YES];
}

-(void)moreButtonAction:(UIButton *)sender
{
    isloadclass = !isloadclass;
    if(isloadclass)
    {
        
        self.myView.frame = CGRectMake(0, self.myView.frame.origin.y, self.myView.frame.size.width+215, self.myView.frame.size.height);
        
        if (self.m_rightview==nil) {
            CGRect r = CGRectMake(self.myView.frame.size.width-215, 0, 215, self.myView.frame.size.height);
            self.m_rightview = [[ClassListViewRightClasssearchView alloc] initWithFrame:r];
            
            self.m_rightview.str_name_0 = self.str_name_0;
            self.m_rightview.str_name_1 = self.str_name_1;
            
            [self.m_rightview getusedata:[[dicclasslist objectForKey:@"data"] objectForKey:@"all_class"]];
            
            [self.myView addSubview:self.m_rightview]; //添加右侧试图
        }
        self.m_rightview.backgroundColor= [UIColor whiteColor];
        self.m_rightview.tag = 1987;
        self.m_rightview.delegate = self;
        self.m_rightview.userInteractionEnabled = YES;
        
        if (self.m_btn_left_back==nil) {
            CGRect r = CGRectMake(0, 0, self.myView.frame.size.width-215, self.myView.frame.size.height);
            self.m_btn_left_back = [[UIButton alloc] initWithFrame:r];
            [self.myView addSubview:self.m_btn_left_back];
        }
        self.m_btn_left_back.tag = 7743;
        self.m_btn_left_back.backgroundColor = [UIColor blackColor];
        self.m_btn_left_back.alpha = 0.0;
        [self.m_btn_left_back addTarget:self
                                 action:@selector(backgounderbuttondown:)
                       forControlEvents:UIControlEventTouchDown];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.m_btn_left_back.alpha = 0.5;
            self.myView.frame = CGRectMake(-215,self.myView.frame.origin.y, self.myView.frame.size.width, self.myView.frame.size.height);
        } completion:^(BOOL finished) {
            ;
        }];
        
        
        
    }
    else
    {
        [UIView animateWithDuration:0.3 animations:^{
            self.myView.frame = CGRectMake(0, self.myView.frame.origin.y, self.myView.frame.size.width-215, self.myView.frame.size.height);
            [self.myView viewWithTag:7743].alpha = 0.0;
        } completion:^(BOOL finished) {
            //[[self.myView viewWithTag:7743] removeFromSuperview];
            //[[self.myView viewWithTag:1987] removeFromSuperview];
        }];
    }
}
-(void)backgounderbuttondown:(UIButton*)button //遮罩点击返回原来的分类商品列表
{
    isloadclass = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.myView.frame = CGRectMake(0, self.myView.frame.origin.y, self.myView.frame.size.width-215, self.myView.frame.size.height);
        button.alpha = 0.0;
    } completion:^(BOOL finished) {
        //[button removeFromSuperview];
        //[[self.myView viewWithTag:1987] removeFromSuperview];

    }];

}

-(void)backButtonAction:(UIButton *)sender
{
    if(isloadclass)
    {
        isloadclass = NO;
        [UIView animateWithDuration:0.1 animations:^{
            self.myView.frame = CGRectMake(0, self.myView.frame.origin.y, self.myView.frame.size.width-215, self.myView.frame.size.height);
            [self.myView viewWithTag:7743].alpha = 0.0;
        } completion:^(BOOL finished) {
            [[self.myView viewWithTag:7743] removeFromSuperview];
            [[self.myView viewWithTag:1987] removeFromSuperview];
            [self.navigationController popViewControllerAnimated:YES];
        }];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)searchclassindex:(NSString *)string  title:(NSString *)titlestring//点击右侧筛选完毕的分类id
{
    [self setTitle:titlestring];
    isloadclass = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.myView.frame = CGRectMake(0,
                                       self.myView.frame.origin.y,
                                       self.myView.frame.size.width-215,
                                       self.myView.frame.size.height);
        [self.myView viewWithTag:7743].alpha = 0.0;

    } completion:^(BOOL finished) {
        //[[self.myView viewWithTag:7743] removeFromSuperview];
        //[[self.myView viewWithTag:1987] removeFromSuperview];
        
        if(dicdataparam)
        {
            [dicdataparam removeAllObjects];
        }
        dicdataparam = [[NSMutableDictionary alloc] init];
        [dicdataparam setObject:string forKey:@"cid"];
        
        
        [self.view startLoadingAnimation];
        self.m_b_page_index=1;
        
        [[HttpNewBaseService sharedClient] getRequestOperation:@"goods/goods_list" paramDictionary:dicdataparam onCompletion:^(NSString *responseString) {
            
            [self.view stopLoadingAnimation];
            
            @try {
                NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                dicclasslist = [[NSMutableDictionary alloc] initWithDictionary:jsondic copyItems:YES];
                if([jsondic objectForKey:@"data"]&&([[[jsondic objectForKey:@"data"] objectForKey:@"goods"] isKindOfClass:[NSArray class]]))
                {
                    NSArray * arr = [[jsondic objectForKey:@"data"] objectForKey:@"goods"];
                    self.m_b_is_loading=NO;
                    if ([arr count] >= 10) {
                        self.m_b_have_more=YES;
                    } else {
                        self.m_b_have_more=NO;
                    }
                    if (data_list_no_search) {
                        [data_list_no_search removeAllObjects];
                        [data_list_no_search addObjectsFromArray:arr];
                    } else {
                        data_list_no_search=[[NSMutableArray alloc] initWithArray:arr copyItems:YES];
                    }
                    
                    //init
                    if (collectionview)
                    {
                        [collectionview reloadData];
                    }
                    else
                    {
                        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
                        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
                        flowLayout.footerReferenceSize=CGSizeMake(self.view.frame.size.width, 50);
                        //注册
                        collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width,self.myView.frame.size.height) collectionViewLayout:flowLayout];
                        [collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
                        collectionview.delegate = self;
                        collectionview.dataSource  = self;
                        collectionview.backgroundColor = [UIColor whiteColor];
                        [collectionview registerClass:[ClassListFooter class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"ClassListFooter"];
                        [self.myView addSubview:collectionview]; //分类列表
                    }
                    collectionview.contentOffset=CGPointMake(0, 0);
                    
                    self.view_no_data_b.hidden=YES;
                } else {
                    if (collectionview)
                    {
                        [collectionview removeFromSuperview];
                        collectionview = nil;
                    }
                    //[self showAlertView:@"温馨提示" subtitle:@"没有找到商品"];
                    self.view_no_data_b.hidden=NO;
                }
            } @catch (NSException *exception) {
                //[URBAlertView dialogWithTitle:@"温馨提示" subtitle:@"服务器异常！"];
                [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
            }
        } onError:^(NSError *error) {
            [self.view stopLoadingAnimation];
            [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
        } onAnimated:YES]; //分类列表
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //加载更多
    {
        CGRect frame = scrollView.frame;
        CGSize size = scrollView.contentSize;
        CGPoint point = scrollView.contentOffset;
        
        if (point.y > (size.height-frame.size.height)) {
            if (self.m_b_have_more && self.m_b_is_loading==NO) {
                self.m_b_is_loading = YES;
                [self loadMoreData];
            }
        }
    }
}

- (void)loadMoreData
{
    if (self.issearch) {
        
        if (dicdataparam) {
            [dicdataparam setObject:[NSNumber numberWithInteger:++self.m_b_page_index] forKey:@"p"];
        }
        
        [[HttpNewBaseService sharedClient] getRequestOperation:@"index/search" paramDictionary:dicdataparam onCompletion:^(NSString *responseString) {

            @try {
                NSDictionary *jsondic = [NSJSONSerialization JSONObjectWithData:[responseString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
                
                NSDictionary * dic_data = [jsondic objectForKey:@"data"];
                
                if(dic_data && [dic_data objectForKey:@"goods"]&&([[dic_data objectForKey:@"goods"] isKindOfClass:[NSArray class]]))
                {
                    NSArray * arr = [dic_data objectForKey:@"goods"];
                    self.m_b_is_loading=NO;
                    if ([arr count] >= 10) {
                        self.m_b_have_more=YES;
                    } else {
                        self.m_b_have_more=NO;
                    }
                    
                    if (data_list) {
                        [data_list addObjectsFromArray:arr];
                    } else {
                        data_list = [[NSMutableArray alloc] initWithArray:arr copyItems:YES];
                    }
                    
                    if (collectionview) {
                        [collectionview reloadData];
                    } else {
                        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
                        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
                        //注册
                        collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width,self.myView.frame.size.height) collectionViewLayout:flowLayout];
                        [collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
                        collectionview.delegate = self;
                        collectionview.dataSource  = self;
                        collectionview.backgroundColor = [UIColor whiteColor];
                        [self.myView addSubview:collectionview]; //分类列表
                    }
                    
                } else {
                    [self showAlertView:@"温馨提示" subtitle:@"没有找到对应的商品！"];
                    self.m_b_have_more=NO;
                    self.m_b_is_loading=NO;
                    if (collectionview) {
                        [collectionview reloadData];
                    }
                }
            } @catch (NSException *exception) {
                [self showAlertView:@"温馨提示" subtitle:@"服务器异常！"];
            }@finally {}
        } onError:^(NSError *error) {
            [self showAlertView:@"温馨提示" subtitle:@"网络断了！"];
            self.m_b_have_more=NO;
            self.m_b_is_loading=NO;
            if (collectionview) {
                [collectionview reloadData];
            }
        } onAnimated:YES]; //分类列表
        
    } else {
        if (dicdataparam) {
            [dicdataparam setObject:[NSNumber numberWithInteger:++self.m_b_page_index] forKey:@"p"];
        }
        
        [[HttpNewBaseService sharedClient] getRequestOperation:@"goods/goods_list" paramDictionary:dicdataparam onCompletion:^(NSString *responseString) {

            @try {
                NSDictionary * jsondic = [NSJSONSerialization JSONObjectWithData:
                                          [responseString dataUsingEncoding:NSUTF8StringEncoding]
                                                                         options:NSJSONReadingMutableLeaves error:nil];

                if([jsondic objectForKey:@"data"]&&([[[jsondic objectForKey:@"data"] objectForKey:@"goods"] isKindOfClass:[NSArray class]]))
                {
                    NSArray * arr = [[jsondic objectForKey:@"data"] objectForKey:@"goods"];
                    self.m_b_is_loading=NO;
                    if ([arr count] >= 10) {
                        self.m_b_have_more=YES;
                    } else {
                        self.m_b_have_more=NO;
                    }
                    if (data_list_no_search) {
                        [data_list_no_search addObjectsFromArray:arr];
                    } else {
                        data_list_no_search=[[NSMutableArray alloc] initWithArray:arr copyItems:YES];
                    }
                    
                    //init
                    if (collectionview)
                    {
                        [collectionview reloadData];
                    }
                    else
                    {
                        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
                        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
                        //注册
                        collectionview = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.myView.frame.size.width,self.myView.frame.size.height) collectionViewLayout:flowLayout];
                        [collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
                        collectionview.delegate = self;
                        collectionview.dataSource  = self;
                        collectionview.backgroundColor = [UIColor whiteColor];
                        [self.myView addSubview:collectionview]; //分类列表
                    }
                    
                } else {
                    //[URBAlertView dialogWithTitle:@"温馨提示" subtitle:@"服务器异常！"];
                    self.m_b_have_more=NO;
                    self.m_b_is_loading=NO;
                    if (collectionview) {
                        [collectionview reloadData];
                    }
                }
            } @catch (NSException *exception) {
                [self showAlertView:@"温馨提示" subtitle:@"服务器异常!"];
            }
        } onError:^(NSError *error) {
            [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
            self.m_b_have_more=NO;
            self.m_b_is_loading=NO;
            if (collectionview) {
                [collectionview reloadData];
            }
        } onAnimated:YES]; //分类列表
    }
}

@end
























